package com.swpu.service_remind;

import com.alibaba.fastjson.JSONObject;
import com.swpu.service_remind.config.RabbitExChangeConfig;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

//消息推送的模拟方法
@Controller
@RequestMapping(value = "/api/wx/xcx")
public class WxApi {

 //   Logger logger = LoggerFactory.getLogger(WxApi.class);

    @Autowired
    private WxCommonService wxCommonService;
    @Value("${wx.appid}")
    private String appid;
    @Value("${wx.appsecret}")
    private String appsecret;


    @Resource
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/sendSms")
    private void sendSms() throws InterruptedException {
        String msg = "HelloWorld rabbitmq";
        for (Integer i = 0; i < 10; i++) {
            CorrelationData correlationData = new CorrelationData(i.toString());
            rabbitTemplate.convertAndSend(RabbitExChangeConfig.DESTINATION_NAME, RabbitExChangeConfig.SMS_ROUTING_KEY, msg + i, correlationData);
        }
    }




    @RequestMapping(value = "sendMsg")
    @ResponseBody
    public String sendMsg(HttpServletRequest request){
        //String token = request.getParameter("token");
        //请求 微信接口 获取 accessToken
        String accessToken = wxCommonService.refreshAccessToken(appid,appsecret);
        String openid = "oScK060PdN8P6o2s_ehDFozw_Oy0";
        String templateId = "k3psfAWSe6jJnEqU29LTBj8J-U_JfOmx7MIOr6QGPO0";
        String page = "点击消息的跳转路径";
        // 构建订阅消息内容的JSON对象
        // 构建订阅消息内容的JSON对象
        JSONObject messageData = new JSONObject();
        messageData.put("thing1", createDataItem("温馨提示", "您有新的提醒"));
        messageData.put("character_string3", createDataItem("当前饮水量", "1233"));
        messageData.put("phrase4", createDataItem("距离上次饮水", "小时"));
        messageData.put("thing5", createDataItem("目标饮水量", "张三"));
        messageData.put("time6", createDataItem("饮水时间", "2019年10月1日 15:01"));
        // 将订阅消息内容转换为JSON字符串
        String jsonData = messageData.toJSONString();
        WeChatSubscribeMessageSender.sendSubscribeMessage(accessToken,openid,templateId,page,jsonData);
        return "1234556";
    }
    private static Map<String, Object> createDataItem(String name, String value) {
        Map<String, Object> item = new HashMap<>();
        item.put("value", value);
        return item;
    }
}

